package com.atues.runner;

import com.atues.domain.entity.Article;
import com.atues.mapper.ArticleMapper;
import com.atues.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Component
public class ViewCountRunner implements CommandLineRunner {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RedisCache redisCache;
    @Override
    public void run(String... args) throws Exception {
        //查询博客信息 id viewCount
        List<Article> articles = articleMapper.selectList(null);

        Map<String, Integer> viewCountMap = articles.stream().collect(Collectors.toMap(article ->  article.getId().toString(),
                article-> article.getViewCount().intValue()));

        redisCache.setCacheMap("article:viewCount",viewCountMap);

    }


}
